<?php
namespace Bus\Controller;
use Think\Controller;
use Think\Model;
class SearchController extends Controller {
	public function search()
	{
		layout('base');
		$this->display();
	}

	public function searchHandle()
	{
		$sname=I('station_name');

		$lineid=0;
		if(is_numeric($sname))
		{
			if($sname==(int)$sname)
			{
				$lineid=$sname;
				//dump($lineid);
			}
			else
			{
				echo "<div id='errormsg'>请检查线路名称是否输入正确</div>";
			}
		}
		elseif (is_numeric(str_replace("路","",$sname)))
		{
			if(str_replace("路","",$sname)==(int)str_replace("路","",$sname))
			{
				$lineid=str_replace("路","",$sname);
				//dump(str_replace("路","",$sname));
			}
			else
			{
				echo "<div id='errormsg'>请检查线路名称是否输入正确</div>";
			}
		}
		// else
		// {
		// 	echo "没有执行上面语句?";
		// }


		if($lineid<>0)
		{
			$station=D('line_up_down');
			$station->where("lineid=$lineid");

			$info=$station->select();

			//$pointx=112.43452477;
			//$pointy=34.6412782;
			// if(isset($_POST['sub'])){  
			// 	$pointx = $_POST['pointx'];  
			// 	$pointy = $_POST['pointy']; 
			// }
			//$pointx2 = $_POST['pointx'];  
			//$pointy2 = $_POST['pointy']; 
			//$info[0]['pointx']=$pointx2;
			//$info[0]['pointy']=$pointy2;
			//$info[1]['pointx']=$pointx2;
			//$info[1]['pointy']=$pointy2;

			if(count($info)==0){
				echo "<div id='errormsg'>请检查线路名称是否输入正确</div>";
			}
			//dump($info);
			$this->assign('searchbus',$info);
			layout('base');
			$this->display('search2');
		}
		else
		{
			$station=D('qr');
			$station->where("station_name like '%$sname%'");

			$info=$station->group('station_name')->select();
			if(count($info)==0){
				echo "<div id='errormsg'>请检查站点名称是否输入正确</div>";
			}
			$this->assign('searchbus',$info);
			layout('base');
			$this->display('search');
		}




	}

	public function getlocation()
	{
		$this->display();

	}

	public function nearest()
	{
		$lng = floatval($_GET['lng']);
		$lat = floatval($_GET['lat']);
		//dump(is_numeric($lng));

		//$subQuery=D('station_info')->field('station_name')->where("station_flag=1 and latitude>$lat-0.008 and  latitude<$lat+0.008 and longitude>$lng-0.008 and longitude<$lng+0.008")->buildSql();
		//$info=D('qr')->where("station_name in ".$subQuery)->group('station_name')->select();
		$info=D('qr')->where("station_flag=1 and latitude>$lat-0.008 and  latitude<$lat+0.008 and longitude>$lng-0.008 and longitude<$lng+0.008")
		->join('ad_station_info  ON ad_qr.station_name=ad_station_info.station_name')->group('ad_qr.station_name')->select();
		//dump($info);
		$this->assign('searchbus',$info);
		layout('base');
		$this->display('nearest');

	}
}
